﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Objects Exercise 1</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Functions for working with shapes in  standard Planar coordinate system.</h1>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function createPoint(newX, newY) {
            return {
                X: newX,
                Y: newY,
                toString: function () {
                    return "(" + this.X + "," + this.Y + ")";
                }
            }
        }

        function createLine(newP1, newP2) {
            return {
                P1: newP1,
                P2: newP2,
                toString: function () {
                    return "(" + this.P1 + "," + this.P2 + ")";
                }
            }
        }

        function distanceBetween(P1, P2) {
            return Math.sqrt(Math.pow((P2.X - P1.X), 2) + Math.pow((P2.Y - P1.Y), 2));
        }

        function lineLength(L) {
            return distanceBetween(L.P1, L.P2);
        }

        function checkTriangle(L1, L2, L3) {
            var a = lineLength(L1);
            var b = lineLength(L2);
            var c = lineLength(L3);

            if ((a + b > c)
                && (a + c > b)
                && (b + c > a))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        var Point1 = createPoint(-2, -3);
        var Point2 = createPoint(-4, 4);

        jsConsole.writeLine("Distance P1:" + Point1 + " P2:" + Point2 + ": " + distanceBetween(Point1, Point2));

        var Line1 = createLine(Point1, Point2);
        var Line2 = createLine(Point1, Point2);
        var Line3 = createLine(Point1, Point2);
        
        jsConsole.writeLine("Triangle possible L1:" + Line1 + " L2:" + Line2 + " L3:" + Line3 + ": " + checkTriangle(Line1, Line2, Line3));
    </script>
</body>
</html>
